package ar.edu.unq.wannaplay.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import org.joda.time.Interval;
import org.springframework.transaction.annotation.Transactional;

import ar.edu.unq.wannaplay.dtos.LicenceSpitterDTO;
import ar.edu.unq.wannaplay.dtos.TypeOfLicenceDTO;
import ar.edu.unq.wannaplay.employee.Employee;
import ar.edu.unq.wannaplay.exceptions.EmployeeCantTakeLicenceException;
import ar.edu.unq.wannaplay.exceptions.NegativeNumberException;
import ar.edu.unq.wannaplay.helpers.LicenceSpitter;
import ar.edu.unq.wannaplay.persistence.daos.LicenceSpitterDAO;
import ar.edu.unq.wannaplay.utils.Pair;
import ar.edu.unq.wannaplay.utils.Service;

public class LicenceService implements Service<LicenceSpitter, LicenceSpitterDTO> {
    private static final long serialVersionUID = 1L;

    private LicenceSpitterDAO dao;

    @Override
    public LicenceSpitterDTO toDto(final LicenceSpitter object) {
        throw new UnsupportedOperationException();
    }

    private LicenceSpitter getLicenceSpitter() {
        return this.getDao().getUnique();
    }

    @Transactional
    public void saveLicenceSpitter(final LicenceSpitter licenceSpitter) {
        this.getDao().save(licenceSpitter);
    }

    @Transactional
    public void giveLicenceToEmployee(final String licenceName, final Employee emp, final Interval licenceInterval)
            throws EmployeeCantTakeLicenceException {
        this.getDao().giveLicenceToEmployee(licenceName, emp, licenceInterval);
    }

    @Transactional
    public void addLicenceType(final String newLicence, final int anualMax, final int consecutiveMax)
            throws NegativeNumberException {
        this.getDao().addLicenceType(newLicence, anualMax, consecutiveMax);
    }

    @Transactional
    public List<TypeOfLicenceDTO> getAllTypesOfLicencesDTO() {
        List<TypeOfLicenceDTO> typesDTO = new ArrayList<TypeOfLicenceDTO>();
        LicenceSpitter spitter = this.getLicenceSpitter();

        for (Entry<String, Pair<Integer, Integer>> entry : spitter.getLicencesInformation().entrySet()) {
            typesDTO.add(new TypeOfLicenceDTO(entry.getKey(), entry.getValue().getFirst(), entry.getValue().getSecond()));
        }

        return typesDTO;
    }

    public LicenceSpitterDAO getDao() {
        return dao;
    }

    public void setDao(final LicenceSpitterDAO dao) {
        this.dao = dao;
    }
}
